<?php
/**
 * ePDQ results handler
 *
 * @package page
 * @copyright Copyright 2008 Kuroi Web Design
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: epdq_cpi_handler.php 263 2008-10-30 02:01:11Z kuroi $
 */

require('includes/application_top.php');

if (EPDQ_REDIRECT_WORKING) {

  if (isset($_GET['oid']) && !empty($_GET['oid'])) {
    $sql = "select transactionstatus
            from " . TABLE_EPDQ_LOG . "
            where oid='" . $_GET['oid'] . "'
            order by timestamp desc
            limit 1";
    $txn = $db->Execute($sql);
    if ($txn->RecordCount() <= 0) {
      $fatalError = 'temporary order ID ' . $_GET['oid'] . ' not recognised in ePDQ log table';
    } elseif (substr($txn->fields['transactionstatus'],0,7) != 'Success') {
      $paymentWarning = 'Payment ' . strtolower($txn->fields['transactionstatus']);
    }
  } else {
    $fatalError = 'Temporary order ID not passed as parameter from ePDQ';
  }

  if ($fatalError) {
    $logFile = DIR_FS_CATALOG . 'epdq/logs/epdq_' . date('ymdHis') . '.log';
    if (file_exists($logFile)) unlink($logFile);
    $handle = fopen($logFile, 'a');
    fwrite($handle, $fatalError . "\n");
    fclose($handle);
    echo '<meta HTTP-EQUIV="REFRESH" content="0; url=technical_problem.html">';
  } elseif ($paymentWarning) {
    $messageStack->add_session('checkout_payment', $paymentWarning, 'error');
    echo '<meta HTTP-EQUIV="REFRESH" content="0; url=index.php?main_page=checkout_payment">';
  } else {
    echo '<meta HTTP-EQUIV="REFRESH" content="0; url=index.php?main_page=checkout_success">';
  }

} else {

  exit('<meta HTTP-EQUIV="REFRESH" content="0; url=index.php?main_page=checkout_process&oid=' . $_GET['oid'] . '">');

}
?>